package com.digital.welcome.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.digital.welcome.entity.Fee;
import com.digital.welcome.entity.Pay;
import com.digital.welcome.entity.User;
import com.digital.welcome.mapper.PayMapper;
import com.digital.welcome.mapper.UserMapper;
import com.digital.welcome.response.Result;
import com.digital.welcome.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class PayServiceImpl extends ServiceImpl<PayMapper, Pay> implements PayService {

    @Autowired
    private PayMapper payMapper;

    @Override
    public Pay selectPayByUserName(String userName) {
        return payMapper.selectPayByUserName(userName);
    }

    @Override
    public String getPayNumber() {
        Integer count = null;
        QueryWrapper<Pay> queryWrapper = new QueryWrapper<>();
        count = payMapper.selectCount(queryWrapper);
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("查询成功").setData(count));
    }

    @Override
    public boolean insertPay(Pay pay) {

        return false;
        // return payMapper.insertPay(pay.getUserName());
    }

    @Deprecated
    @Override
    public String payByUserName(Fee fee, String userName) {
        String orderNo = IdUtil.getSnowflake().nextIdStr();
        String payUrl = "http://localhost:7070/alipay/pay?subject=" + fee.getCategory() + "&traceNo=" + orderNo + "&totalAmount=" + fee.getAmount();
        Pay pay = new Pay();
        pay.setUserName(userName);
        pay.setAmount(fee.getAmount());
        pay.setPaytype(fee.getCategory());
        pay.setTradeNo(orderNo);
        pay.setState(0);
        payMapper.insert(pay);
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("支付页面").setData(payUrl));

    }

    @Override
    public Boolean getState(String userName) {
        QueryWrapper<Pay> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", userName);
        queryWrapper.eq("state", 1);
        Integer count = payMapper.selectCount(queryWrapper);
        return count != 0;
    }

    @Autowired
    private UserMapper userMapper;

    @Override
    public String getStudents() {
        List<User> userList = userMapper.getPayStudents();
        for (User user : userList) {
            String type = payMapper.getType(user.getUserName());
            if (type == null) {
                user.setPaytype("绿色");
            } else {
                user.setPaytype(type);
            }
        }
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("查询成功").setData(userList));
    }

    @Override
    public String getNStudents() {
        List<User> userList = userMapper.getNPayStudents();
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("查询成功").setData(userList));
    }

    @Override
    public String getCollegeNStudents(String college) {
        if (college == null) {
            return JSON.toJSONString(new Result<>().setCode(400).setMessage("查询失败"));
        }
        List<User> userList = userMapper.getCollegeNPayStudents(college);
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("查询成功").setData(userList));
    }

    @Override
    public String getCollegeStudents(String college) {
        if (college == null) {
            return JSON.toJSONString(new Result<>().setCode(400).setMessage("查询失败"));
        }
        List<User> userList = userMapper.getCollegePayStudents(college);
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("查询成功").setData(userList));
    }

    @Override
    public String selectConfirm(String userName) {
        User user = userMapper.getUserByUsername(userName);
        if (user == null) {
            return JSON.toJSONString(new Result<>().setCode(400).setMessage("学生不存在"));
        }
        Map<String,Object> map = new HashMap<>();
        map.put("username", user.getUserName());
        map.put("name", user.getName());
        map.put("sex", user.getSex());
        map.put("identity", user.getIdentity());
        Pay pay = payMapper.selectPayInformation(userName);
        if (pay == null) {
            map.put("green", "普通");
            map.put("confirm", 0);
        } else {
            map.put("green", pay.getPaytype());
            map.put("confirm", pay.getState());
        }
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("查询成功").setData(map));
    }

    @Override
    public String scenePay(User user) {
        if (user != null && user.getUserName() != null) {
            Pay pay = new Pay();
            pay.setState(1);
            pay.setUserName(user.getUserName());
            payMapper.insert(pay);
            return JSON.toJSONString(new Result<>().setCode(1).setMessage("缴费成功"));
        }
        return JSON.toJSONString(new Result<>().setCode(400).setMessage("信息错误"));
    }
}
